import axios from "axios"
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import { BASE_URL, TIME_OUT } from "./config"
class CyRequest {
  instance: AxiosInstance
  constructor(baseURL: string, timeout: number) {
    this.instance = axios.create({
      baseURL,
      timeout
    })

    this.instance.interceptors.response.use((res) => {
      return res.data
    }, err => {
      return err
    })
  }

  request(config: AxiosRequestConfig): Promise<AxiosResponse> {
    return this.instance.request(config)
  }

  get<T = any>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
    return this.instance.request({ ...config, method: 'GET' })
  }

  post(config: AxiosRequestConfig): Promise<AxiosResponse> {
    return this.instance.request({ ...config, method: 'POST' })
  }

  delete(config: AxiosRequestConfig): Promise<AxiosResponse> {
    return this.instance.request({ ...config, method: 'DELETE' })
  }

  patch(config: AxiosRequestConfig): Promise<AxiosResponse> {
    return this.instance.request({ ...config, method: 'PATCH' })
  }
}

export default new CyRequest(BASE_URL, TIME_OUT)